package com.llyb.listener;


import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class MqListener {

/*
    // simple
    @RabbitListener(queues = {"com.queue.simple"})
    public void processSimple(String msg, Message message){
        System.out.println(msg);
        System.out.println(message);
    }

    // work 队列
    @RabbitListener(queues = {"com.queue.work"})
    public void processWork1(String msg, Message message){
        System.out.println("processWork1 : "+msg);
    }
    @RabbitListener(queues = {"com.queue.work"})
    public void processWork2(String msg, Message message){
        System.out.println("processWork2 : "+msg);
    }

    // fanout 队列
    @RabbitListener(queues = {"com.queue.fanout01"})
    public void processFanout1(String msg, Message message){
        System.out.println("粉丝1 收到消息: "+msg);
    }

    @RabbitListener(queues = {"com.queue.fanout02"})
    public void processFanout2(String msg, Message message){
        System.out.println("粉丝2 收到消息: ======="+msg);
    }

    // routing 工作模式
    @RabbitListener(queues = {"com.queue.routing01"})
    public void processRouting(String msg){
        System.out.println("routing 接收到："+msg);
    }
    @RabbitListener(queues = {"com.queue.routing02"})
    public void processRouting2(String msg){
        System.out.println("routing222 接收到："+msg);
    }

    // Topic 工作模式
    @RabbitListener(queues = {"com.queue.topic01"})
    public void processTopic(String msg){
        System.out.println("Topic 接收到："+msg);
    }
    @RabbitListener(queues = {"com.queue.topic02"})
    public void processTopic2(String msg){
        System.out.println("Topic222 接收到："+msg);
    }
*/


    // 消费者消息确认可靠
    @RabbitListener(bindings = {
            @QueueBinding(
                    // 配置队列信息：durable 为 true 表示队列持久化；autoDelete 设置为 false 表示关闭自动删除
                    value = @Queue(value = "queue.security.consumer",durable = "true", autoDelete = "false"),
                    // 配置交换机信息：durable 为 true 表示队列持久化；autoDelete 设置为 false 表示关闭自动删除
                    exchange = @Exchange(value = "exchange.security.consumer",durable = "true",autoDelete = "false"),
                    key = ""
            )})
    public void  processorConsumerAck(String dataString, Message message, Channel channel) throws IOException {

        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            log.info("消费者接收到消息：{}",dataString);

            System.out.println(1/0);

            channel.basicAck(deliveryTag,false);
        } catch (Exception e) {
            Boolean redelivered = message.getMessageProperties().getRedelivered();

            if (!redelivered) {
                channel.basicNack(deliveryTag,false,true);
            }else {
                // 使用拒绝策略
                channel.basicReject(deliveryTag,false);
            }
        }

    }

}
